package com.data.modules.log.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.data.common.api.vo.Result;
import com.data.common.constant.WebsocketConst;
import com.data.common.util.SpringContextUtils;
import com.data.modules.log.entity.AICommand;
import com.data.modules.log.entity.AIDTO;
import com.data.modules.log.entity.WsLog;
import com.data.modules.log.mapper.WsLogMapper;
import com.data.modules.log.service.IWsLogService;
import com.data.modules.message.websocket.WebSocket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
import java.util.List;

/**
 * @Description: WS日志
 * @Author: data-boot
 * @Date:   2022-03-07
 * @Version: V1.0
 */
@Slf4j
@Service
public class WsLogServiceImpl extends ServiceImpl<WsLogMapper, WsLog> implements IWsLogService {
    @Autowired
    private WebSocket webSocket;
    @Autowired
    private WsLogMapper wsLogMapper;
    @Override
    public void pubScript(WsLog wsLog) throws Exception {
        AIDTO dto = new AIDTO();
        if(WebsocketConst.AI_SAVE.equalsIgnoreCase(wsLog.getDeviceStatus())){
            //执行保存脚本命令
            dto.setCommand(WebsocketConst.AI_SAVE);
        }else if(WebsocketConst.AI_RERUN.equalsIgnoreCase(wsLog.getDeviceStatus())){
            dto.setCommand(WebsocketConst.AI_RERUN);
        }else if(WebsocketConst.AI_RUN.equalsIgnoreCase(wsLog.getDeviceStatus())){
            dto.setCommand(WebsocketConst.AI_RUN);
        }else if(WebsocketConst.AI_STOPALL.equalsIgnoreCase(wsLog.getDeviceStatus())){
            dto.setCommand(WebsocketConst.AI_STOPALL);
        }else if(WebsocketConst.AI_STOP.equalsIgnoreCase(wsLog.getDeviceStatus())){
            dto.setCommand(WebsocketConst.AI_STOP);
        }else{
            //默认直接执行
            dto.setCommand(WebsocketConst.AI_RUN);
        }
        // 调用WS 推送脚本至 指定的客户端

        String script = wsLog.getCodeInfo();

        dto.setScript(script);
        dto.setId(wsLog.getId());
        dto.setName(wsLog.getCodeLog());

        AICommand command = new AICommand();
        command.setData(dto);
        command.setType(WebsocketConst.AI_COMMAND);
        // 判断userId 是否在线
        String userId = wsLog.getDeviceId();
        Session session = WebSocket.sessionPool.get(userId);
        if (session == null || !session.isOpen()) {
            log.error(userId+" WsLogServiceImpl send Error session is down");
        }
        String msg = JSON.toJSONString(command);
        log.info("===pubScript userId:{},msg:{}",userId,msg);
        webSocket.sendMessage(userId, msg);
        //TODO 更新状态
    }

    @Override
    public int heartbeatDetection(String deviceId) {
        return wsLogMapper.heartbeatDetection(deviceId);
    }

    @Override
    public List<String> findData(){
        return wsLogMapper.findData();
    }

    @Override
    public int truncateTable() {
        return wsLogMapper.truncateTable();
    }


}
